package huawei.js2025;

/**
 * 比较两个版本号的大小
 */
public class Question8 {


    public static void main(String[] args) {
        String version1 = "1.0.001";
        String version2 = "1.0.1";
        System.out.println(compareVersion(version1, version2));
    }

    public static int compareVersion(String version1, String version2) {
        String[] v1 = version1.split("\\.");
        String[] v2 = version2.split("\\.");
        for (int i = 0; i < Math.max(v1.length, v2.length); i ++) {
            String subV1 = doVersion(i < v1.length ? v1[i] : "0");
            String subV2 = doVersion(i < v2.length ? v2[i] : "0");

            int i1 = compareSubVersion(subV1, subV2);
            if (i1 != 0) {
                return i1;
            }
        }

        return 0;
    }

    private static int compareSubVersion(String subV1, String subV2) {

        for (int i = 0; i < Math.min(subV1.length(), subV2.length()); i++) {
            int convertIntVal1 = getConvertIntVal(subV1.charAt(i));
            int convertIntVal2 = getConvertIntVal(subV2.charAt(i));

            if (convertIntVal1 > convertIntVal2) {
                return 1;
            } else if (convertIntVal1 < convertIntVal2) {
                return -1;
            }
        }

        if (subV1.length() > subV2.length()) {
            return 1;
        } else if (subV1.length() < subV2.length()) {
            return -1;
        } else {
            return 0;
        }
    }

    public static String doVersion(String version) {
        String s = version.replaceFirst("^0+", "");
        return s.isEmpty() ? "0" : s;
    }

    public static int getConvertIntVal(char c) {
        if (c >= 'a' && c <= 'z') {
            return c - 'a';
        } else if (c >= 'A' && c <= 'Z') {
            return c - 'A' + 26;
        } else if (c >= '0' && c <= '9') {
            return c - '0' + 52;
        }

        return -1;
    }
}
